<?php
/*----------------------------------------------------------------------
 * 代理商管理
 * Author: zheng.wang
----------------------------------------------------------------------*/

namespace Admin\Controller;

use Think\Controller;
class AgentController extends AdminController {
    
    //代理商列表
    public function agentList(){
		/*查询条件*/ 
		//有效期查询
		if($_GET['create_time_start'] && $_GET['create_time_end']){
			$map['agent_time'] = array(array('EGT',strtotime($_GET['create_time_start'])  ),array('ELT',strtotime($_GET['create_time_end'])+86400)) ;
		}elseif($_GET['create_time_start']){
			$map['agent_time'] = array('EGT',strtotime($_GET['create_time_start']));
		}elseif($_GET['create_time_end']){
			$map['agent_time'] = array('ELT',strtotime($_GET['create_time_end'])+86400);
		}
		//代理商状态
		if($_GET['agent_status'] != 0){
			$map['agent_status'] = array('EQ',$_GET['agent_status']);
		}
		//代理商地区
		if($_GET['mutil_level_data_1_id_1']){
			$map['area_1'] = array('EQ',$_GET['mutil_level_data_1_id_1']);
		}
		if($_GET['mutil_level_data_1_id_2']){
			$map['area_2'] = array('EQ',$_GET['mutil_level_data_1_id_2']);
		}
		if($_GET['mutil_level_data_1_id_3']){
			$map['area_3'] = array('EQ',$_GET['mutil_level_data_1_id_3']);
		}		
		
		//代理商名称
		$agentName = $_GET['agent_name'];
		if($agentName == '0' ){ 
			$map['agent_name'] = array('like',"%$agentName%");
		}
		if($_GET['agent_name']){
			$map['agent_name'] = array('like',"%$agentName%");
		}		
		$count  = D('agent')->where($map)->count();// 查询满足要求的总记录数
		$Page   = new \Think\Page2($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
		$show   = $Page->show();// 分页显示输出
		$list   = D('agent')->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
//echo D('agent')->getLastSql();

		//查询代理商影院数据量
		$agentIds = array();
		$agentlist = D('agent')->where($map)->select();
		foreach ($agentlist as $v ){
			array_push($agentIds,$v['id']);
		}
		$temp['agent_id']= array('in',$agentIds);
		$cinemaCount = M('cinema_host')->where($temp)->count();
		
		$page_title = '代理商列表';
		$this->assign('page_title',$page_title);  
		$this->assign('list',$list);    //数据列表
		$this->assign('count',$count);  //代理商数量
		$this->assign('cinemaCount',$cinemaCount);  //代理商影院数量
		$this->assign('page',$show);    //分页
        $this->display();
    }
	
	//添加代理商页面
	public function add(){
		
		//2016计费方式
		$billing_video_play_type = C('AGENT_BILLING_VIDEO_PLAY_TYPE');
		$billing_video_play_type = array(0=>'请选择') + $billing_video_play_type;
		
		$page_title = '添加代理商';
		$this->assign('page_title',$page_title);
		$this->assign('billing_video_play_type',$billing_video_play_type);
		$this->display();
	}
	
	//添加代理商提交程序
	public function insert(){
		$data = D('agent')->create();
		$images = I('request.images');
		if($data){
			$data['agent_time'] = time();
			$data['agent_status'] = 1;  //代理商状态: 1正常  2停用
			$data['agent_honor'] = serialize($images);
			$data['agent_starttime'] = strtotime($data['agent_starttime']);
			$data['agent_endtime'] = strtotime($data['agent_endtime']);
			$data['area_1'] = I('request.mutil_level_data_1_id_1');
			$data['area_2'] = I('request.mutil_level_data_1_id_2');
			$data['area_3'] = I('request.mutil_level_data_1_id_3');
			
			//2016计费方式
			$data['billing_video_play_type'] = I('post.billing_video_play_type');
			
			$data['auth_pic'] = I('request.auth_pic');
			$data['auth_code_1'] = I('request.auth_code_1');
			$data['auth_code_2'] = I('request.auth_code_2');
			
			$result = D('agent')->add($data);
			if($result !== false){
				$msg['status'] = 1 ;
				$msg['info'] = '添加代理商成功' ;
				$msg['url'] = U('Admin/Agent/agentList') ;
			}
		}
		$this->ajaxReturn($msg);
	}
	
	//编辑代理商信息
	public function edit(){
		$map['id'] = I('request.id');
		$info = D('agent')->where($map)->find();
		$info['agent_honor'] = unserialize($info['agent_honor']);
		
		//2016计费方式
		$billing_video_play_type = C('AGENT_BILLING_VIDEO_PLAY_TYPE');
		$billing_video_play_type = array(0=>'请选择') + $billing_video_play_type;
		
		$page_title = '代理商编辑';
		$this->assign('page_title',$page_title);
		$this->assign('info',$info);
		$this->assign('billing_video_play_type',$billing_video_play_type);
		$this->display();
		
	}
	//编辑带来商提交程序
	public function update(){
		$data = D('agent')->create();
		$map['id'] = I('request.id');
		$images = I('request.images');
		if(!empty($images['0']) || !empty($images['1']) || !empty($images['2'])){
			$data['agent_honor'] = serialize($images);	
		}else{
			$data['agent_honor'] = '';
		}		
		if($data){
			$data['agent_starttime'] = strtotime($data['agent_starttime']);
			$data['agent_endtime'] = strtotime($data['agent_endtime']);
			$data['area_1'] = I('request.mutil_level_data_1_id_1');
			$data['area_2'] = I('request.mutil_level_data_1_id_2');
			$data['area_3'] = I('request.mutil_level_data_1_id_3');
			
			//2016计费方式
			$data['billing_video_play_type'] = I('post.billing_video_play_type');
			
			$data['auth_pic'] = I('request.auth_pic');
			$data['auth_code_1'] = I('request.auth_code_1');
			$data['auth_code_2'] = I('request.auth_code_2');
		}
		$result = D('agent')->where($map)->save($data);
		if($result !== false){
			$msg['status'] = 1 ;
			$msg['info'] = '修改信息成功' ;
			$msg['url'] = U('Admin/Agent/agentList') ;
		}
		$this->ajaxReturn($msg);
	}
	
	//编辑带来商提交程序
	public function detail(){
		$map['id'] = I('request.id');
		$info = D('agent')->where($map)->find();
		$info['agent_honor'] = unserialize($info['agent_honor']);
		$page_title = '代理商详情';
		$this->assign('page_title',$page_title);		
		$this->assign('info',$info);	
		$this->display();
	}

	
	//停用代理商
	public function stop(){
		$agent_status = 2;  //代理商状态: 1正常  2停用
		$map['id'] = I('request.id');
		$result = D('agent')-> where($map)->setField('agent_status',$agent_status);
		if($result != false){
			$msg['info'] = '代理商已经停用';
			$msg['status'] = 1;
		}else{
			$msg['info'] = '代理商停用失败';
			$msg['status'] = 2;
		}
		$this->ajaxReturn($msg);
	}	
	
 	//启用代理商
	public function start(){
		$agent_status = 1;  //代理商状态: 1正常  2停用
		$map['id'] = I('request.id');
		$result = D('agent')-> where($map)->setField('agent_status',$agent_status);
		if($result != false){
			$msg['info'] = '代理商已经启用';
			$msg['status'] = 1;
		}else{
			$msg['info'] = '代理商启用失败';
			$msg['status'] = 2;
		}
		$this->ajaxReturn($msg);
	}

	//代理商影院
	public function cinema(){
		$map['agent_id'] = I('request.id');
		$count  = M('cinema_host')->where($map)->count();// 查询满足要求的总记录数
		$Page   = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
		$show   = $Page->show();// 分页显示输出
		$list   = M('cinema_host')->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$page_title = '代理商影院信息';
		$this->assign('page_title',$page_title);  
		$this->assign('list',$list);
		$this->assign('page',$show);		
		$this->display();
	}
	
}